
package battleship;

/**
 *
 * @author Shaun Pickford
 */
public class ShipPlacement {

    Conversion conversionObj = new Conversion();
    int[] endPoints = new int[4];
    String[] directions = new String[4];

    public void ShipPlacement()
    {
    }

    public void checkShipPlacement(Ship ship, int startCoordinate, char who)
    {
        if(who == 'o')
        {

        /* Clear arrays */
        for(int xx = 0; xx < endPoints.length; xx++)
        {
            endPoints[xx] = -1;
            directions[xx] = "";

        }
        //String[] directions = new String[4];
        int upEndPoint, downEndPoint, leftEndPoint, rightEndPoint;
        boolean canGoUp, canGoDown, canGoLeft, canGoRight;

        /* Calculate endpoints based on starting coordinate */
        upEndPoint = startCoordinate - ((ship.shipLength - 1) * 10);
        downEndPoint = startCoordinate + ((ship.shipLength - 1) * 10);
        leftEndPoint = startCoordinate - (ship.shipLength - 1) ;
        rightEndPoint = startCoordinate + (ship.shipLength - 1);
        //System.out.println("upendPoint: " + upEndPoint + " downEndPoint: " + downEndPoint + " leftEndPoint: " + leftEndPoint + " rightEndPoint: " + rightEndPoint);

        /* Determine if endpoints are valid */
        canGoUp = validateOpponentEndPoints("U", upEndPoint, startCoordinate, ship);
        canGoDown = validateOpponentEndPoints("D", downEndPoint, startCoordinate, ship);
        canGoLeft = validateOpponentEndPoints("L", leftEndPoint, startCoordinate, ship);
        canGoRight = validateOpponentEndPoints("R", rightEndPoint, startCoordinate, ship);

        /* If up endPoint is valid */
        if(canGoUp)
        {
            endPoints[0] = upEndPoint;
            directions[0] = "U";
            //System.out.println("can go up!");
        }//can go up

        /* If down endPoint is valid */
        if(canGoDown)
        {
            endPoints[1] = downEndPoint;
            directions[1] = "D";
            //System.out.println("can go down!");

        }//can go down

        /* If left endPoint is valid */
        if(canGoLeft)
        {
            endPoints[2] = leftEndPoint;
            directions[2] = "L";
            //System.out.println("can go left!");
        }//can go left

        /* If right endPoint is valid */
        if(canGoRight)
        {
            endPoints[3] = rightEndPoint;
            directions[3] = "R";
            //System.out.println("can go right!");
        }//can go right

        }//if opponent


        /*If Player*/
        else
        {
            /* Clear arrays */
        for(int xx = 0; xx < endPoints.length; xx++)
        {
            endPoints[xx] = -1;
            directions[xx] = "";

        }
        //String[] directions = new String[4];
        int upEndPoint, downEndPoint, leftEndPoint, rightEndPoint;
        boolean canGoUp, canGoDown, canGoLeft, canGoRight;

        /* Calculate endpoints based on starting coordinate */
        upEndPoint = startCoordinate - ((ship.shipLength - 1) * 10);
        downEndPoint = startCoordinate + ((ship.shipLength - 1) * 10);
        leftEndPoint = startCoordinate - (ship.shipLength - 1) ;
        rightEndPoint = startCoordinate + (ship.shipLength - 1);
        //System.out.println("upendPoint: " + upEndPoint + " downEndPoint: " + downEndPoint + " leftEndPoint: " + leftEndPoint + " rightEndPoint: " + rightEndPoint);

        /* Determine if endpoints are valid */
        canGoUp = validatePlayerEndPoints("U", startCoordinate, upEndPoint, ship);
        canGoDown = validatePlayerEndPoints("D", startCoordinate, downEndPoint, ship);
        canGoLeft = validatePlayerEndPoints("L", startCoordinate, leftEndPoint, ship);
        canGoRight = validatePlayerEndPoints("R", startCoordinate, rightEndPoint, ship);

        /* If up endPoint is valid */
        if(canGoUp)
        {
            endPoints[0] = upEndPoint;
            directions[0] = "U";
            //System.out.println("can go up!");
        }//can go up

        /* If down endPoint is valid */
        if(canGoDown)
        {
            endPoints[1] = downEndPoint;
            directions[1] = "D";
            //System.out.println("can go down!");

        }//can go down

        /* If left endPoint is valid */
        if(canGoLeft)
        {
            endPoints[2] = leftEndPoint;
            directions[2] = "L";
            //System.out.println("can go left!");
        }//can go left

        /* If right endPoint is valid */
        if(canGoRight)
        {
            endPoints[3] = rightEndPoint;
            directions[3] = "R";
            //System.out.println("can go right!");
        }//can go right


        }//if player

    }//checkShipPlacement

    private boolean validateOpponentEndPoints(String direction, int endPoint, int startingPoint, Ship ship)
    {
        boolean valid = true;
        
        /* Check to see if endpoint is on the board. */
        if(endPoint < 1  || endPoint > 100)
        {
            valid = false;
            //System.out.println("Endpoint is off the board!");
        }

        /* Check to see if endpoint is on the same row if a horizontal endPoint is being determined */
        if(direction.equalsIgnoreCase("L") || direction.equalsIgnoreCase("R"))
        {
            String startCoordinate = conversionObj.toCoordinate(startingPoint);
            //System.out.println("startCoordiante is " + startCoordinate);
            String endCoordinate = conversionObj.toCoordinate(endPoint);
            //System.out.println("endCoordiante is " + endCoordinate);
            String temp1 = startCoordinate.substring(0,1);
            if(!endCoordinate.equals("error"))
            {
                String temp2 = endCoordinate.substring(0,1);
            if(temp1.equals(temp2))
            {
                valid = true;
                //System.out.println("start and end coordinates are on the same row!");
            }
            else
            {
                valid = false;
            }
                
            }//if endCoordinate is a valid coordiante
            
        }//if horizontal check

        /*Check for collisions*/
        if(valid)
        {
            if(direction.equalsIgnoreCase("U"))
            {
                for(int x = startingPoint - 1; x >= endPoint - 1; x = x - 10)
                {
                    if(x > 0 && x <= 100)
                    {
                        char temp = Main.gameBoard.opponentGameBoard[x];
                        if(temp != 'N')
                        {
                            valid = false;
                            //System.out.println("collision up! temp is " + temp);
                            break;
                        }
                    }
                }
            }//direction is up

            if(direction.equalsIgnoreCase("D"))
            {
                for(int j = startingPoint - 1; j <= endPoint - 1; j = j + 10)
                {
                    if(j > 0 && j <= 100)
                    {
                        char temp = Main.gameBoard.opponentGameBoard[j];
                        if(temp != 'N')
                        {
                            valid = false;
                            //System.out.println("collision down! temp is " + temp);
                            break;
                        }
                    }
                }
            }//direction is down

            if(direction.equalsIgnoreCase("L"))
            {
                for(int k = startingPoint - 1; k >= endPoint - 1; k--)
                {
                    if(k > 0 && k <= 100)
                    {
                        char temp = Main.gameBoard.opponentGameBoard[k];
                        if(temp != 'N')
                        {
                            valid = false;
                            //System.out.println("collision left! temp is " + temp);
                            break;
                        }
                    }
                }//for
            }//direction is left

            if(direction.equalsIgnoreCase("R"))
            {
                for(int l = startingPoint - 1; l <= endPoint - 1; l++)
                {
                    if(l > 0 && l <= 100)
                    {
                        char temp = Main.gameBoard.opponentGameBoard[l];
                        if(temp != 'N')
                        {
                            valid = false;
                            //System.out.println("collision right! temp is " + temp);
                            break;
                        }
                    }
                }
            }//direction is right
               
        }//if valid

        return valid;

    }//validate end points

    private boolean validatePlayerEndPoints(String direction, int startingPoint, int endPoint, Ship ship)
    {
        boolean valid = true;

        /* Check to see if endpoint is on the board. */
        if(endPoint < 1 || endPoint > 100)
        {
            valid = false;
            return valid;
            //System.out.println("Endpoint is off the board!");
        }

        /* Check to see if endpoint is on the same row if a horizontal endPoint is being determined */
        if(direction.equalsIgnoreCase("L") || direction.equalsIgnoreCase("R"))
        {
            String startCoordinate = conversionObj.toCoordinate(startingPoint);
            //System.out.println("startCoordiante is " + startCoordinate);
            String endCoordinate = conversionObj.toCoordinate(endPoint);
            //System.out.println("endCoordiante is " + endCoordinate);
            String temp1 = startCoordinate.substring(0,1);
            if(!endCoordinate.equals("error"))
            {
                String temp2 = endCoordinate.substring(0,1);
            if(temp1.equals(temp2))
            {
                valid = true;
                //System.out.println("start and end coordinates are on the same row!");
            }
            else
            {
                valid = false;
            }

            }//if endCoordinate is a valid coordiante

        }//if horizontal check

        /*Check for collisions*/
        if(valid)
        {
            if(direction.equalsIgnoreCase("U"))
            {
                for(int x = startingPoint; x >= endPoint; x = x - 10)
                {
                    if(x > 0 && x <= 100)
                    {
                        char temp = Main.gameBoard.gameBoard[x - 1];
                        if(temp != 'N')
                        {
                            valid = false;
                            //System.out.println("collision up! temp is " + temp);
                            return valid;
                        }
                    }
                }
            }//direction is up

            if(direction.equalsIgnoreCase("D"))
            {
                for(int j = startingPoint; j <= endPoint; j = j + 10)
                {
                    if(j > 0 && j <= 100)
                    {
                        char temp = Main.gameBoard.gameBoard[j - 1];
                        if(temp != 'N')
                        {
                            valid = false;
                            //System.out.println("collision down! temp is " + temp);
                            return valid;
                        }
                    }
                }
            }//direction is down

            if(direction.equalsIgnoreCase("L"))
            {
                for(int k = startingPoint; k >= endPoint; k--)
                {
                    if(k > 0 && k <= 100)
                    {
                        char temp = Main.gameBoard.gameBoard[k - 1];
                        if(temp != 'N')
                        {
                            valid = false;
                            //System.out.println("collision left! temp is " + temp);
                            return valid;
                        }
                    }
                }//for
            }//direction is left

            if(direction.equalsIgnoreCase("R"))
            {
                for(int l = startingPoint; l <= endPoint; l++)
                {
                    if(l > 0 && l <= 100)
                    {
                        char temp = Main.gameBoard.gameBoard[l - 1];
                        if(temp != 'N')
                        {
                            valid = false;
                            //System.out.println("collision right! temp is " + temp);
                            return valid;
                        }
                    }
                }
            }//direction is right

        }//if valid

        return valid;

    }//validate player end points

    public void placeOpponentShips(String direction, int startingPoint, int endPoint, Ship ship)
    {
        /* Reduce value of startingPoint and endPoint for array placement */
        startingPoint = startingPoint - 1;
        endPoint = endPoint - 1;
        //System.out.println("Placing " + ship.getShipName());
        if(direction.equalsIgnoreCase("U"))
            {
                for(int x = startingPoint; x >= endPoint; x = x - 10)
                {
                    Main.gameBoard.opponentGameBoard[x] = ship.getShipID();

                }
            }//direction is up

            if(direction.equalsIgnoreCase("D"))
            {
                for(int j = startingPoint; j <= endPoint; j = j + 10)
                {
                    Main.gameBoard.opponentGameBoard[j] = ship.getShipID();

                }
            }//direction is down

            if(direction.equalsIgnoreCase("L"))
            {
                for(int k = startingPoint; k >= endPoint; k--)
                {
                    Main.gameBoard.opponentGameBoard[k] = ship.getShipID();

                }
            }//direction is left

            if(direction.equalsIgnoreCase("R"))
            {
                for(int l = startingPoint; l <= endPoint; l++)
                {
                    Main.gameBoard.opponentGameBoard[l] = ship.getShipID();

                }
            }//direction is right


    }

    public void placePlayerShips(String direction, int startingPoint, int endPoint, Ship ship)
    {
        /* Reduce value of startingPoint and endPoint for array placement */
        startingPoint = startingPoint - 1;
        endPoint = endPoint - 1;
        //System.out.println("Placing " + ship.getShipName());
        if(direction.equalsIgnoreCase("U"))
            {
                for(int x = startingPoint; x >= endPoint; x = x - 10)
                {
                    Main.gameBoard.gameBoard[x] = ship.getShipID();

                }
            }//direction is up

            if(direction.equalsIgnoreCase("D"))
            {
                for(int j = startingPoint; j <= endPoint; j = j + 10)
                {
                    Main.gameBoard.gameBoard[j] = ship.getShipID();

                }
            }//direction is down

            if(direction.equalsIgnoreCase("L"))
            {
                for(int k = startingPoint; k >= endPoint; k--)
                {
                    Main.gameBoard.gameBoard[k] = ship.getShipID();

                }
            }//direction is left

            if(direction.equalsIgnoreCase("R"))
            {
                for(int l = startingPoint; l <= endPoint; l++)
                {
                    Main.gameBoard.gameBoard[l] = ship.getShipID();

                }
            }//direction is right



    }

    public String[] getPossibleDirections()
    {
        return directions;
    }

    public int[] getPossibleEndPoints()
    {
        return endPoints;
    }
        
}//class
